var readline = require( 'readline' )
var fs = require( 'fs' )

//创建readline接口实例
var rl = readline.createInterface( {
  input: process.stdin,
  output: process.stdout
} )
var config = {}
// question方法
rl.question( "程序端口：(8181)", function( port ) {
  console.log( "port:" + ( port || 8181 ) )
  config.port = port || 8181
  rl.question( "服务端接口：(https://wechat.shangning.site/api)", function( baseUrl ) {
    console.log( "baseUrl:" + ( baseUrl || 'https://wechat.shangning.site/api' ) )
    config.baseUrl = baseUrl || 'https://wechat.shangning.site/api'
    rl.question( "系统版本：(wechat)", function( version ) {
      console.log( "version:" + ( version || 'wechat' ) )
      config.version = version || 'wechat'
      rl.close()
    })
  })
})
// close事件监听
rl.on( "close", function() {
  var bucket = {
    fly: 'fasion',
    wechat: 'wechatfasion',
    hc: 'hcfasion',
    ae: 'aefasion',
    nan: 'nanfasion'
  }
  var text = 'var bucket = ' + JSON.stringify(bucket) + ';'
  text += `module.exports = {port:${config.port} ,version: '${config.version}' ,baseUrl: '${config.baseUrl}', bucket: bucket['${config.version}']}`
  console.log(text)
  fs.writeFile('./config.js', text, function (err) {
    if (err) {
      console.log('添加配置失败。请手动添加')
      return
    }
    console.log('添加配置成功')
    console.log('\u001b[31m ' + '执行 pm2 start production.js --name="[name]" 启动程序' + ' \u001b[39m')
    process.exit(0)
  })
})